package com.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import modle.User;
import util.JsonChange;
import util.CardPlay;
import modle.Card;
import modle.CardGame;
import modle.CardGames;

/**
 * Servlet implementation class LandlordJudgementServlet
 */

@WebServlet("/LandlordJudgementServlet")
/**
 * 将用户是否抢地主通告于此，若用户不抢则判断哪个人机抢到地主，并将人机出牌返回前端。
 * @author WXE
 *
 */
public class LandlordJudgementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LandlordJudgementServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String judgement = request.getParameter("Judgement");
		String result = null;
		List<Card> robot0 = null;
		List<Card> robot2 = null;
		Map<String, Object> map = new HashMap<>(16);
		User user = (User) request.getSession().getAttribute("user");
		CardGames cardGames = (CardGames) this.getServletContext().getAttribute("CardGames");
		
		System.out.println(user.toString());
		int cardGameID = user.getCardGameID();
		Map<Integer, CardGame> games = cardGames.getCardGames();
		CardGame cardGame = games.get(cardGameID);
		System.out.println(cardGame.toString());
		String flag = "1" ;
		if( flag.equals(judgement) ){
			CardPlay.jugdeLord(1 , cardGame);
			map.put("playerList1", cardGame.getPlayerList()[1]);
			result = "1";
		}else {
			CardPlay.jugdeLord(0, cardGame);
			int f = cardGame.getLandLordsFlag();
			if( f == 0 ) {
				result = f +"";
				robot0 = JsonChange.toCard(CardPlay.comptuerShowCards(0, cardGame));
				cardGame.getFlag()[0] = true;
				cardGame.getCurrentList()[0] = robot0;
				robot2 = null;
			}else {
				result =  f +"";
				robot2 = JsonChange.toCard(CardPlay.comptuerShowCards(2, cardGame));
				cardGame.getFlag()[2] = true;
				cardGame.getCurrentList()[2] = robot2;
				robot0 = JsonChange.toCard(CardPlay.comptuerShowCards(0, cardGame));
				if( robot0 == null ) {
					cardGame.getFlag()[0] = false;
					cardGame.getCurrentList()[0] = robot0;
					System.out.println("0不出");
				}else {
					cardGame.getFlag()[0] = true;
					cardGame.getCurrentList()[0] = robot0;
				}
			}
			map.put("a0", robot0);
			map.put("a2", robot2);
		}
		map.put("result", result);
		response.getWriter().println(JSON.toJSONString(map));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
